package com.alks.common.config.interceptor;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alks.entity.data.entity.BatchOrderUpload;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ExcelListener extends AnalysisEventListener<BatchOrderUpload> {
    /**
     * 批处理阈值，正式环境通常为2000
     */
    private static final int BATCH_COUNT = 1000;
    /** ExcelModel表格列属性模板 */
    @Getter
    private List<BatchOrderUpload> list = new ArrayList<>(BATCH_COUNT);
    private Logger logger = LoggerFactory.getLogger("日志");



    /**
     * 每解析到一条数据，执行一次invoke（）方法
     * @param data
     * @param context
     */
    @Override
    public void invoke(BatchOrderUpload data, AnalysisContext context) {
        logger.info("解析到一条数据：{}", JSON.toJSONString(data));
        list.add(data);

        if (list.size() >= BATCH_COUNT) {
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        logger.info("共有{}条Id数据存入list", list.size());
    }

}
